#include "idt.h"

idt_entry_t idt[256];
idt_ptr_t   idt_ptr;

void install_idt_table(void)
{
	//initPIC(); // PIC initialisieren, komplett demaskieren

	idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1;
	idt_ptr.base  = (unsigned long) &idt;

	// memset?!?

	//shellPrint("idt_ptr ist %x groß!\n", sizeof(idt_ptr));

	add_idt_table( 0, (unsigned long)intr_stub_0, 0x08, 0x8E);
	add_idt_table( 1, (unsigned long)intr_stub_1, 0x08, 0x8E);
	
	initPIC();
	
	add_idt_table( 32, (unsigned long)irq_0, 0x08, 0x8E);
	add_idt_table( 33, (unsigned long)irq_1, 0x08, 0x8E);
	add_idt_table( 34, (unsigned long)irq_2, 0x08, 0x8E);

	idt_flush((unsigned long)&idt_ptr);
}

void add_idt_table(unsigned char num, unsigned long base,
		   unsigned short sel, unsigned char flags)
{
	idt[num].baseLo = base & 0xFFFF;
	idt[num].baseHi = (base >> 16) & 0xFFFF;
	idt[num].selector = sel;
	idt[num].immerNull = 0;
	idt[num].flags = flags;
}

void *irq_routines[16] =
{
	0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0
};

void irq_install_handler(int irq, void (*handler)(struct cpu_state* cpu))
{
	irq_routines[irq] = handler;
}

void handle_interrupt(struct cpu_state* cpu)
{
	shellPrint("Exception %d abgefangen!\n", cpu->intr);
}

void irq_handler(struct cpu_state* cpu)
{
	void (*handler)(struct cpu_state* cpu);

	//shellPrint("interrupt: %d\n", cpu->intr);
	handler = irq_routines[cpu->intr];//[cpu->intr - 32];
	if (handler)
	{
		handler(cpu);
	}

	if (cpu->intr >= 8)
	{
		outB(0xA0, 0x20);
	}

	outB(0x20, 0x20);
}


